package com.student.Activitys.activity;

import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.Gson;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.adapter.PartyApplyExpandableAdapter;
import com.student.Activitys.bean.InfoReturnBean;
import com.student.Activitys.bean.MemberFamilyBean;
import com.student.Activitys.bean.UserBean;
import com.student.Activitys.bean.pack.PartyApplyPack;
import com.odd.Activitys.http.manager.PartyManager;
import com.student.Activitys.utils.AccountValidatorUtils;
import com.student.Activitys.utils.Constant;
import com.student.Activitys.utils.ToastUtils;
import com.student.Activitys.view.PinnedHeaderExpandableListView;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**PartyManager
 * 项目名称：智慧校园学生端
 * 类描述：入党申请
 * 创建人：张飞祥
 * 创建时间：2017/10/12 9:11
 * 修改人：张飞祥
 * 修改时间：2017/10/12 9:11
 * 修改备注：
 */
public class PartyApplyActivity extends BaseActivity {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_party_apply_title)
    TextView tvPartyApplyTitle;
    @Bind(R.id.ex_list_party_apply)
    PinnedHeaderExpandableListView exListPartyApply;
    @Bind(R.id.btn_party_apply_submit)
    Button btnPartyApplySubmit;
    @Bind(R.id.layout_party_submit)
    FrameLayout layoutPartySubmit;


    //适配器
    private PartyApplyExpandableAdapter adapter = null;
    //数据源
    private List<PartyApplyPack> list = null;
    //标题 集合
    private String[] titles = {"个人信息","家庭成员","入党志愿","本人经历"};
    //当前用户
    private UserBean user = null;
    //初始化网络请求
    public PartyManager mApiManager;
    //弹窗 diaolog
    public MaterialDialog msgDialog;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_party_apply);
        //网络请求 常驻方法
        mApiManager = mApiManager.getPartyApiManager(PartyApplyActivity.this, PartyApplyActivity.this.getTaskId());
        //设置标题
        tvTitle.setText("入党申请");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        //获取当前用户
        /**
         * 创建 Gson 解析储存的用户数据 反填 界面数据
         */
        Gson gson = new Gson();
        user = gson.fromJson(mACache.getAsJSONObject(Constant.USER) + "", UserBean.class);
    }

    @Override
    protected void initData() {
        /**
         * 列表初始化
         */
        for (int i = 0; i < titles.length; i++) {
            PartyApplyPack pack = new PartyApplyPack();
            if (i == 0) {
                pack.setUser(user);
            }
            pack.setName(titles[i]);
            pack.setType(i);
            pack.setFamily(new MemberFamilyBean());
            list.add(pack);
        }
        //设置悬浮头部VIEW
        exListPartyApply.setHeaderView(getLayoutInflater().inflate(R.layout.item_pulic_group_head, exListPartyApply, false));
        adapter = new PartyApplyExpandableAdapter(list, PartyApplyActivity.this, exListPartyApply);
        exListPartyApply.setAdapter(adapter);
    }

    @Override
    protected void getNotData() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_return, R.id.btn_party_apply_submit})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:                   //退出
                finish();
                break;
            case R.id.btn_party_apply_submit:    //申请
                setDateSubmit();
                break;
        }
    }

    /**
     * 数据提交
     */
    private void setDateSubmit() {

        //获取最新 adapter 发生变化的数据
        List<PartyApplyPack> adapterList = adapter.getAdapterList();
        //获取 用户信息
        UserBean adapterUser = adapterList.get(0).getUser();
        //获取 家人信息
        MemberFamilyBean family = adapterList.get(1).getFamily();
        //获取 入党志愿
        String volunteer = adapterList.get(2).getVolunteer();
        //获取 本人经历
        String experience = adapterList.get(3).getExperience();



        /**
         * 判断 验证 列表 （家庭住址、入党志愿、本人经历）不允许为空，才可以请求
         */
        if ("".equals(volunteer) || volunteer == null ||
            "".equals(experience) || experience == null ||
            "".equals(adapterUser.getMobile()) || adapterUser.getMobile() == null ||
            "".equals(adapterUser.getMail()) || adapterUser.getMail() == null ||
            "".equals(adapterUser.getSFZH()) || adapterUser.getSFZH() == null ||
            "".equals(adapterUser.getFamilyAddr()) || adapterUser.getFamilyAddr() == null) {
            ToastUtils.showShort(PartyApplyActivity.this, "请完善您的信息情况~");
        } else if (!AccountValidatorUtils.isMobile(adapterUser.getMobile())) {
            ToastUtils.showShort(PartyApplyActivity.this, "手机号码格式不正确！");
        } else if (!AccountValidatorUtils.isEmail(adapterUser.getMail())) {
            ToastUtils.showShort(PartyApplyActivity.this, "邮箱格式不正确！");
        } else {

            /**
             * 判断当前实体那个 为空 并添加 空值
             */

            //父亲名字
            if (family.getFatherName()==null)
                family.setFatherName("");
            //父亲手机号
            if (family.getFatherPhoneNumber()==null)
                family.setFatherPhoneNumber("");
            //父亲工作单位
            if (family.getFatherCompany()==null)
                family.setFatherCompany("");
            //父亲职务
            if (family.getFatherPost()==null)
                family.setFatherPost("");
            //母亲名字
            if (family.getMotherName()==null)
                family.setMotherName("");
            //母亲手机号
            if (family.getMotherPhoneNumber()==null)
                family.setMotherPhoneNumber("");
            //母亲工作单位
            if (family.getMotherCompany()==null)
                family.setMotherCompany("");
            //母亲职务
            if (family.getMotherPost()==null)
                family.setMotherPost("");
            //提交数据
            initHttp(adapterUser,family,volunteer,experience);
        }
    }

    /**
     * 开始网络请求
     */
    private void initHttp(UserBean adapterUser,MemberFamilyBean family, String situation, String expression) {
        //请求日课表数据
        mApiManager.partyApply(Constant.HTTP_SUPPORT_APPLY_ACTION, adapterUser.getXH(),
                adapterUser.getMobile(), adapterUser.getMail(), adapterUser.getQQ(), adapterUser.getWeixin(),
                adapterUser.getFamilyAddr(), adapterUser.getSFZH(), family.getFatherName(),family.getFatherPhoneNumber(),
                family.getFatherCompany(),family.getFatherPost(),family.getMotherName(),family.getMotherPhoneNumber(),
                family.getMotherCompany(),family.getMotherPost(),situation, expression);
        //显示 加载dialog
        initShowTimeDialog("正在提交申请，请稍后...",PartyApplyActivity.this);
    }

    /**
     * 思政：入党申请 数据提交 网络请求 加载是否成功
     */
    @Subscriber(tag = Constant.BUS_PARTY_SUBMIT_ON_COMPLETED, mode = ThreadMode.MAIN)
    public void initOnCompleted(String str) {
        //销毁handler
        time_handler.removeCallbacks(time_runnable);
        timeDailog.dismiss();
        dialog.dismiss();
    }

    /**
     * 思政：入党申请 数据提交 网络请求 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_PARTY_SUBMIT, mode = ThreadMode.MAIN)
    public void initSubmit(InfoReturnBean bean) {
        msgDialog = new MaterialDialog.Builder(PartyApplyActivity.this)
                .title("成功")
                .content(bean.getMsg())
                .positiveText("确认")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(MaterialDialog dialog, DialogAction which) {
                        finish();
                    }
                }).show();
        // 设置点击屏幕Dialog不消失
        msgDialog.setCanceledOnTouchOutside(false);
        //取消Back键监听
        msgDialog.setCancelable(false);
    }

}
